package com.zjw.zy.coreui.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.R
import com.zjw.zy.bean.HourlyWeather
import com.zjw.zy.bean.Weather1Day
import com.zjw.zy.coreui.fragment.adapter.WeatherAdapterHolder
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils
import com.zjw.zy.utils.WeatherUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/27 18:42
 */
class MainWeatherView : BaseViewGroup {

    companion object {
        private const val TAG = "MainWeatherView"
    }

    private val mViewContain: View by lazy { findViewById(R.id.main_weather_view_contain) }
    private val mWeatherStateIcon: ImageView by lazy { findViewById(R.id.main_weather_state_icon) }
    private val mWeatherTempText: TextView by lazy { findViewById(R.id.main_weather_state_temp_text) }
    private val mWeatherFeelTempText: TextView by lazy { findViewById(R.id.main_weather_state_feel_temp_text) }
    private val mWeatherStateText: TextView by lazy { findViewById(R.id.main_weather_state_text) }
    private val mWeatherCityText: TextView by lazy { findViewById(R.id.main_weather_current_city_text) }
    private val mWeatherDateText: TextView by lazy { findViewById(R.id.main_weather_current_date_text) }
    private val mWeatherRainText: TextView by lazy { findViewById(R.id.main_weather_rain_text) }
    private val mWeatherWindText: TextView by lazy { findViewById(R.id.main_weather_wind_text) }
    private val mWeatherRhText: TextView by lazy { findViewById(R.id.main_weather_rh_text) }
    private val mHourList: RecyclerView by lazy { findViewById(R.id.main_weather_hour_list) }
    private val mSunTimeView: SunTimeView by lazy { findViewById(R.id.main_weather_sun_time_view) }
    private val mSunRaiseText: TextView by lazy { findViewById(R.id.main_fragment_sun_raise_text) }
    private val mSunResetText: TextView by lazy { findViewById(R.id.main_fragment_sun_reset_text) }


    private val mHoursAdapter = WeatherAdapterHolder()

    private var mListener: WeatherViewListener? = null

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onLayoutId(): Int {
        return R.layout.main_weather_view_layout
    }

    override fun onBindListener() {
        mWeatherStateIcon.setOnClickListener {
            mListener?.onWeather15DayClick()
        }
    }

    override fun onBindValue() {
        mHourList.adapter = mHoursAdapter
        mHourList.layoutManager = LinearLayoutManager(context)
    }

    override fun onFindView(rootView: View?) {
    }

    private fun setWeatherHourlyInformation(hourlyWeather: HourlyWeather) {
        mWeatherStateText.text = hourlyWeather.text
        mWeatherTempText.text =
            ResourcesUtils.getString(
                R.string.main_weather_temp_text,
                hourlyWeather.tempFc
            )

        mWeatherRainText.text =
            ResourcesUtils.getString(R.string.main_weather_rain_text, "${hourlyWeather.prec}")
        mWeatherWindText.text = ResourcesUtils.getString(
            R.string.main_weather_wind_text,
            hourlyWeather.windDir,
            hourlyWeather.windClass
        )


        mWeatherRhText.text = ResourcesUtils.getString(
            R.string.main_weather_rh_text,
            "${hourlyWeather.rh}%"
        )

        mWeatherFeelTempText.text =
            ResourcesUtils.getString(R.string.main_weather_feel_temp_text, hourlyWeather.feelsLike)
    }

    fun setWeatherInformation(weather: Weather1Day?) {
        if (weather == null) {
            mWeatherCityText.text = ResourcesUtils.getString(R.string.eror_no_weather)
            return
        }

        mWeatherCityText.text = weather.location.name

        setWeatherHourlyInformation(weather.hourlyFcsts[0])
        setWeatherIcon(weather.hourlyFcsts[0])
        mHoursAdapter.notifyData(weather.hourlyFcsts)
    }

    private fun setWeatherIcon(hourlyWeather: HourlyWeather) {
        mWeatherStateIcon.setImageDrawable(WeatherUtils.getWeatherIcon(hourlyWeather))
    }

    fun bindListener(listener: WeatherViewListener) {
        mListener = listener
    }

    fun updateTime(stringOfDay: String?) {
        stringOfDay?.let {
            mWeatherDateText.text = stringOfDay
        }

        mSunTimeView.bindData(TimeUtils.getSunTime(),TimeUtils.currentTime())
        mSunRaiseText.text = TimeUtils.getSunTime().sunRaiseStr
        mSunResetText.text = TimeUtils.getSunTime().sunResetStr
    }


    interface WeatherViewListener {
        fun onWeather15DayClick()
    }
}